package com.qf.service.product.impl;


import com.qf.entity.Product;
import com.qf.entity.User;
import com.qf.mapper.ProductMapper;
import com.qf.service.product.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * --- 永无霸哥 ---
 *
 * @author M.rk
 * @date 2024/12/3 15:22
 * @desc
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;

    //查询所有、模糊查询、分页
    @Override
    public List<Product> productList(Product product) {

        List<Product> products = productMapper.productList(product);


        return products;
    }

    //新增商品数据
    @Override
    public int adddrug(Product product) {

//        System.out.println("product = " + product.getTradeChart());


        //设置添加时，要想后端传入
        //入库时间  创建人
        //更新时间  更新人
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        User user = (User) request.getSession().getAttribute("userInfo");
        String username = user.getUsername();

        product.setFounder(username);
        product.setStockInTime(new Date());
        product.setModifier(username);
        product.setUpdateTime(new Date());

        //不等于空的话进去拆分
        if(product.getTradeChart() != null){
            //截取路径方法
            String path = interceptPath(product.getTradeChart());
            //再装进去
            product.setTradeChart(path);
        }

        int adddrug = productMapper.adddrug(product);
        return adddrug;
    }

    //单个删除
    @Override
    public int handleDelete(int id) {
        int i = productMapper.handleDelete(id);

        return i;
    }

    //批量删除
    @Override
    public int delBatch(ArrayList<Integer> ids) {
        int i = productMapper.delBatch(ids);

        return i;
    }

    //编辑商品信息 redact
    //TODO :修改时间的插入
    @Override
    public int redactProduct(Product product) {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        User user = (User) request.getSession().getAttribute("userInfo");
        String username = user.getUsername();

        product.setModifier(username);
        product.setUpdateTime(new Date());

        System.out.println("product数据是----》"+product);
        int i = productMapper.redactProduct(product);

        return i;
    }


    //截取图片的路径，为了减少数据压力
    public String  interceptPath(String path){
        //通过 / 来划分结果
        String[] split = path.split("/");

        String unite="/";
        for (int i =3;i<=6;i++){
            if(i !=6){
                unite+=split[i]+"/";
            }else {
                unite+=split[i];
            }
        }

        return unite;
    }
}
